package com.woniuxy.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.base.model.entity.BaseMaterial;
import com.woniuxy.base.model.entity.BaseMaterialRecord;
import com.woniuxy.base.service.BaseMaterialService;
import com.woniuxy.response.ResponseResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/baseMaterial")
public class BaseMaterialController {
    @Resource
    private BaseMaterialService baseMaterialService;

    @GetMapping("/all/{current}/{size}")
    public ResponseResult<Page<BaseMaterial>> all(@PathVariable("current") int current, @PathVariable("size") int size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseMaterialService.getAll(current,size));
    }

    @PreAuthorize("hasAuthority('depot:updateIn')")
    @PutMapping("/change")
    public ResponseResult<Boolean> change(@RequestBody BaseMaterial baseMaterial){
        String account = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseMaterialService.update(baseMaterial,account));
    }

    @PreAuthorize("hasAuthority('depot:addIn')")
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody BaseMaterial baseMaterial){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseMaterialService.add(baseMaterial));
    }

    @PostMapping("/conditionalSearch/{current}/{size}")
    public ResponseResult<Page<BaseMaterial>> conditionalSearch(@RequestBody BaseMaterial baseMaterial, @PathVariable("current") Integer current, @PathVariable("size") Integer size){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseMaterialService.conditionSearch(baseMaterial,current,size));
    }

    @GetMapping("/keywordSearch/{current}/{size}")
    public ResponseResult<Page<BaseMaterial>> keywordSearch(@PathVariable("current") Integer current, @PathVariable("size") Integer size, @RequestParam String keyword){

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseMaterialService.keywordSearch(keyword,current,size));
    }

    @GetMapping("/detail")
    public ResponseResult<List<BaseMaterialRecord>> detail(@RequestParam Integer materialId){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseMaterialService.detail(materialId));
    }

    @GetMapping("/baseList")
    public ResponseResult<List<BaseMaterial>> baseList(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",baseMaterialService.baseList());
    }
}
